<?php
 
Kurogo::includePackage('db');

class NotesWebModule extends WebModule
{
    protected $id='notes';
    
    private function usort_callback($a, $b)
    {
        if ( $a['likes'] == $b['likes'] )
            return 0;

        return ( $a['likes'] > $b['likes'] ) ? -1 : 1;
    }
    
    protected function initializeForPage() {
       
        $this->addExternalJavascript('http://code.jquery.com/jquery-1.9.1.min.js');
        $this->addExternalJavascript('http://code.jquery.com/jquery-migrate-1.1.1.min.js');
        $this->addExternalJavascript('\modules\notes\javascript\google-analytics.js');
        $this->addInternalJavascript('\modules\notes\javascript\jquery.mousewheel.min.js');
        $this->addInternalJavascript('\modules\notes\javascript\github.info.js');
        $this->addInternalJavascript('\modules\notes\javascript\tile-slider.js');
        
        $this->addInternalCSS('\modules\notes\css\theme-dark.css');
        $this->addInternalCSS('\modules\notes\css\modern-responsive.css');
        //$this->addInternalCSS('\modules\notes\css\site.css');
        
        $this->controller = DataRetriever::factory('FacebookDataRetriever', array());
        switch ($this->page)
        {
            case 'index':
                $session = $this->getSession();
                if($this->isLoggedIn())
                    $login = true;
                else
                    $login = false;
                $this->assign('loggedin',$login);
                $this->assign('loginurl', self::buildURLForModule('login', 'index'));
                break;
				
	    case 'mapview':
                break;
            
            case 'search':
                break;
            
            case 'postnote':
                $db = new db();
                $sql = "SELECT acronym FROM location WHERE acronym<>'' ORDER BY acronym";
                $result = $db->query($sql, array('acronym'));
                $acronymlist = array();
                while ($row = $result->fetch()) {
                    $acronym = $row['acronym'];
                    $acronymlist[] = $acronym;
                }
                $this->assign('acronyms',$acronymlist);                
                break;
            
            case 'note-submit':
                $db = new db();
                $session = $this->getSession();
                if($this->isLoggedIn()) {
                    $user = $session->getUser();
                    $userID = $user->getUserID();
                    if(isset($_POST[latitude]))
                    {
                        $sql = "INSERT INTO location (latitude, longitude) VALUES ('$_POST[latitude]', '$_POST[longitude]')";
                        $db->query($sql);                     
                        $sql = "SELECT locationID FROM location ORDER BY locationID DESC LIMIT 1";
                        $result = $db->query($sql, array('locationID'));
                        $row = $result->fetch();
                        $locationID = $row['locationID'];                  
                    }
                    else
                    {
                        $sql = "SELECT locationID FROM location WHERE acronym='$_POST[location]'";
                        $result = $db->query($sql,array('locationID'));
                        $row = $result->fetch();
                        $locationID = $row['locationID'];
                    }
                    $message = strip_tags($_POST[message]);
                    $sql = "INSERT INTO post (fbID, message, locationID) VALUES ('$userID',?,'$locationID')";
                    $db->query($sql, array($message)); 
                    $sql = "SELECT postID FROM post WHERE fbID = $userID ORDER BY Date DESC LIMIT 1";
                    $result = $db->query($sql, array('postID'));
                    $row = $result->fetch();
                    $postID = array('postID' => $row['postID']);
                    $this->redirectTo('detail', $postID);
                }
                else
                    $this->redirectToModule('login','index');
                break;
            
            case 'notereply':
                break;
            
            case 'mynotes':
                $db = new db();
                $postlist = array();
                $session = $this->getSession();
                if($this->isLoggedIn()) {
                    $user = $session->getUser();
                    $userID = $user->getUserID();
                
                    $sql = "SELECT message, postID, Date, locationID FROM post WHERE fbID='$userID' ORDER BY Date DESC";
                    $result = $db->query($sql, array('message', 'postID', 'Date', 'locationID'));
                    
                    while ($row = $result->fetch()) {
                        $postID = $row['postID'];
                        $message = $row['message'];
                        $date = $row['Date'];
                        $user = $this->controller->getUser($userID);
                        $photo = 'https://graph.facebook.com/' . $userID . '/picture';
                        //GET LOCATION
                        $locID = $row['locationID'];
                        $s = "SELECT acronym FROM location WHERE locationID = '$locID'";
                        $q = $db->query($s, array('acronym'));
                        if($r = $q->fetch()){
                            $loc = $r['acronym'];
                        }
                        else
                            $loc = "";

                        //LIKE SECTION
                        $session = $this->getSession();
                            if($this->isLoggedIn()){
                                $u = $session->getUser();
                                $uID = $u->getUserID();
                                $login = true;
                                $s = "SELECT * FROM likes WHERE postID = '$postID' AND fbID = '$uID'";
                                $q = $db->query($s,array());
                                $r = $q->fetch();
                                if($r){
                                    $likeimg = '/modules/posts/images/compliant/like.png';
                                }
                                else {
                                    $likeimg = '/modules/posts/images/compliant/nolike.png';
                                }

                                //COUNT LIKES
                                $s = "SELECT COUNT(*) as likes FROM likes WHERE postID = '$postID'";
                                $q = $db->query($s,array());
                                $r = $q->fetch();
                                $likes = $r['likes'];

                                //FLAG SECTION
                                $s = "SELECT * FROM flags WHERE postID = '$postID' AND fbID = '$uID'";
                                $q = $db->query($s,array());
                                $r = $q->fetch();
                                if($r){
                                    $flagimg = '/modules/posts/images/compliant/flag.png';
                                }
                                else {
                                    $flagimg = '/modules/posts/images/compliant/noflag.png';
                                }
                            }
                            else{
                                    $login = false;
                                    $likeimg = false;
                                    $likes = false;
                                    $flagimg = false;
                            }           
                        //COUNT FLAGS
                        $s = "SELECT COUNT(*) as flags FROM flags WHERE postID = '$postID'";
                        $q = $db->query($s,array());
                        $r = $q->fetch();
                        $flags = $r['flags'];

                        if($flags < 5) {
                            $showpost = true;
                        }
                        else {
                            $showpost = false;
                        }

                        //CHECK IF REPLY
                        $s = "SELECT reply_postID, to_postID FROM replies WHERE reply_postID = '$postID'";
                        $q = $db->query($s, array('reply_postID', 'to_postID'));
                        $r = $q->fetch();
                        if($r)
                            $url = $this->buildBreadcrumbURL('detail', array('postID'=>$r['to_postID']));
                        else
                            $url = $this->buildBreadcrumbURL('detail', array('postID'=>$postID));
                        $post = array(
                            'message'=>$message,
                            'username'=>$user['name'],
                            'img'=>$photo,
                            'postID'=>$postID,
                            'likeimg'=>$likeimg,
                            'likes'=>$likes,
                            'flagimg'=>$flagimg,
                            'reveal'=>$showpost,
                            'date'=>$date,
                            'loc'=>$loc,
                            'login'=>$login,
                            'url'=> $url
                        );
                       $postlist[] = $post;
                    }               
                }
                $this->assign('postlist', $postlist);
                break;
            case 'friends':
                $db = new db();
                $postlist = array();
                $session = $this->getSession();
                if($this->isLoggedIn()) {
                    $user = $session->getUser();
                    $userID = $user->getUserID();
                    
                    $friends = $this->controller->getFriends($userID);
                    if($friends) {
                        $sql = "SELECT fbID, message, postID, Date, locationID FROM post WHERE";
                        foreach ($friends['data'] as $key=>$friend) {
                            $friendID = $friend['id'];
                            $sql .= " fbID='$friendID' OR";
                        }
                        $sql .= " fbID='0' ORDER BY Date DESC";
                        $result = $db->query($sql, array('fbID','message', 'postID', 'Date', 'locationID'));
                        while ($row = $result->fetch()) {
                            $userID = $row['fbID'];
                            $postID = $row['postID'];
                            $message = $row['message'];
                            $date = $row['Date'];
                            $user = $this->controller->getUser($userID);
                            $photo = 'https://graph.facebook.com/' . $userID . '/picture';
                            //GET LOCATION
                            $locID = $row['locationID'];
                            $s = "SELECT acronym FROM location WHERE locationID = '$locID'";
                            $q = $db->query($s, array('acronym'));
                            if($r = $q->fetch()){
                                $loc = $r['acronym'];
                            }
                            else
                                $loc = "";

                            //LIKE SECTION
                            $session = $this->getSession();
                                if($this->isLoggedIn()){
                                    $u = $session->getUser();
                                    $uID = $u->getUserID();
                                    $login = true;
                                    $s = "SELECT * FROM likes WHERE postID = '$postID' AND fbID = '$uID'";
                                    $q = $db->query($s,array());
                                    $r = $q->fetch();
                                    if($r){
                                        $likeimg = '/modules/posts/images/compliant/like.png';
                                    }
                                    else {
                                        $likeimg = '/modules/posts/images/compliant/nolike.png';
                                    }

                                    //COUNT LIKES
                                    $s = "SELECT COUNT(*) as likes FROM likes WHERE postID = '$postID'";
                                    $q = $db->query($s,array());
                                    $r = $q->fetch();
                                    $likes = $r['likes'];

                                    //FLAG SECTION
                                    $s = "SELECT * FROM flags WHERE postID = '$postID' AND fbID = '$uID'";
                                    $q = $db->query($s,array());
                                    $r = $q->fetch();
                                    if($r){
                                        $flagimg = '/modules/posts/images/compliant/flag.png';
                                    }
                                    else {
                                        $flagimg = '/modules/posts/images/compliant/noflag.png';
                                    }
                                }
                                else{
                                        $login = false;
                                        $likeimg = false;
                                        $likes = false;
                                        $flagimg = false;
                                }           
                            //COUNT FLAGS
                            $s = "SELECT COUNT(*) as flags FROM flags WHERE postID = '$postID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            $flags = $r['flags'];

                            if($flags < 5) {
                                $showpost = true;
                            }
                            else {
                                $showpost = false;
                            }

                            //CHECK IF REPLY
                            $s = "SELECT reply_postID, to_postID FROM replies WHERE reply_postID = '$postID'";
                            $q = $db->query($s, array('reply_postID', 'to_postID'));
                            $r = $q->fetch();
                            if($r)
                                $url = $this->buildBreadcrumbURL('detail', array('postID'=>$r['to_postID']));
                            else
                                $url = $this->buildBreadcrumbURL('detail', array('postID'=>$postID));
                            $post = array(
                                'message'=>$message,
                                'username'=>$user['name'],
                                'img'=>$photo,
                                'postID'=>$postID,
                                'likeimg'=>$likeimg,
                                'likes'=>$likes,
                                'flagimg'=>$flagimg,
                                'reveal'=>$showpost,
                                'date'=>$date,
                                'loc'=>$loc,
                                'login'=>$login,
                                'url'=> $url
                            );
                           $postlist[] = $post;
                        }
                    }
                }
                $this->assign('postlist', $postlist);
                break;
                
            case 'search-results':
                $location = $_POST['location'];
                $msg = $_POST['message'];
                $query = $_POST['query'];
                $db = new db();
                $postlist = array();
                $sql = "SELECT fbID, message, postID, Date, locationID FROM post";
                if(isset($_POST['nearby'])) {
                    if(!isset($_POST['locations'])) {
                        $this->assign('postlist', $postlist);
                        break;
                    }
                    $locations = $_POST['locations'];
                    $sql .= " WHERE ";
                    foreach($locations as $loc) {
                        $sql .= "locationID='$loc' OR ";
                    }
                    $sql .= "message=''";
                }
                else {
                    if($location || $msg) {
                        if($location && !$msg) {
                            $s = "SELECT locationID FROM location WHERE (acronym='$query' OR name LIKE '%$query%')";
                            $q = $db->query($s, array('locationID'));
                            $sql .= " WHERE (";
                            while($r = $q->fetch()) {
                                $locID = $r['locationID'];
                                $sql .= " locationID='$locID' OR";
                            }
                            $sql .= " locationID='NULL')";
                        }
                        else if($msg && !$location) {
                            $sql .= " WHERE message LIKE '%$query%'";
                        }
                        else {
                            $s = "SELECT locationID FROM location WHERE (acronym='$query' OR name LIKE '%$query%')";
                            $q = $db->query($s, array('locationID'));
                            $r = $q->fetch();
                            $sql .= " WHERE (";
                            while($r = $q->fetch()) {
                                $locID = $r['locationID'];
                                $sql .= " locationID='$locID' OR";
                            }
                            $sql .= " locationID='NULL' ";

                            $sql .= "OR message LIKE '%$query%')";
                        }

                    }
                }
                $sql .= " ORDER BY Date DESC";
                               
                $result = $db->query($sql, array('fbID','message', 'postID', 'Date', 'locationID'));
                while ($row = $result->fetch()) {
                    $userID = $row['fbID'];
                    $postID = $row['postID'];
                    $message = $row['message'];
                    $date = $row['Date'];
                    $user = $this->controller->getUser($userID);
                    $photo = 'https://graph.facebook.com/' . $userID . '/picture';
                    //GET LOCATION
                    $locID = $row['locationID'];
                    $s = "SELECT acronym FROM location WHERE locationID = '$locID'";
                    $q = $db->query($s, array('acronym'));
                    if($r = $q->fetch()){
                        $loc = $r['acronym'];
                    }
                    else
                        $loc = "";
                
                    //LIKE SECTION
                    $session = $this->getSession();
                        if($this->isLoggedIn()){
                            $u = $session->getUser();
                            $uID = $u->getUserID();
                            $login = true;
                            $s = "SELECT * FROM likes WHERE postID = '$postID' AND fbID = '$uID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            if($r){
                                $likeimg = '/modules/posts/images/compliant/like.png';
                            }
                            else {
                                $likeimg = '/modules/posts/images/compliant/nolike.png';
                            }

                            //COUNT LIKES
                            $s = "SELECT COUNT(*) as likes FROM likes WHERE postID = '$postID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            $likes = $r['likes'];

                            //FLAG SECTION
                            $s = "SELECT * FROM flags WHERE postID = '$postID' AND fbID = '$uID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            if($r){
                                $flagimg = '/modules/posts/images/compliant/flag.png';
                            }
                            else {
                                $flagimg = '/modules/posts/images/compliant/noflag.png';
                            }
                        }
                        else{
                                $login = false;
                                $likeimg = false;
                                $likes = false;
                                $flagimg = false;
                        }           
                    //COUNT FLAGS
                    $s = "SELECT COUNT(*) as flags FROM flags WHERE postID = '$postID'";
                    $q = $db->query($s,array());
                    $r = $q->fetch();
                    $flags = $r['flags'];

                    if($flags < 5) {
                        $showpost = true;
                    }
                    else {
                        $showpost = false;
                    }
                    
                    //CHECK IF REPLY
                    $s = "SELECT reply_postID, to_postID FROM replies WHERE reply_postID = '$postID'";
                    $q = $db->query($s, array('reply_postID', 'to_postID'));
                    $r = $q->fetch();
                    if($r)
                        $url = $this->buildBreadcrumbURL('detail', array('postID'=>$r['to_postID']));
                    else
                        $url = $this->buildBreadcrumbURL('detail', array('postID'=>$postID));
                    $post = array(
                        'message'=>$message,
                        'username'=>$user['name'],
                        'img'=>$photo,
                        'postID'=>$postID,
                        'likeimg'=>$likeimg,
                        'likes'=>$likes,
                        'flagimg'=>$flagimg,
                        'reveal'=>$showpost,
                        'date'=>$date,
                        'loc'=>$loc,
                        'login'=>$login,
                        'url'=> $url
                    );
                   $postlist[] = $post;
                }
                $this->assign('postlist', $postlist);
                break;
            case 'detail':
                $postID = $this->getArg('postID');
                $db = new db();
                $note = array();
                //IF ITS A REPLY DONT DISPLAY ANYTHING
                $sql = "SELECT postID FROM post p, replies r WHERE p.postID = r.reply_postID AND p.postID = '$postID'";
                $result = $db->query($sql, array('postID'));
                if($result->fetch()){
                    $replies = array();
                    $this->assign('note', $note); 
                    $this->assign('replies', $replies);
                    $this->assign('reply', 0);
                    if($this->isLoggedIn()){
                        $this->assign('loggedIn', true);
                    }
                    else
                        $this->assign('loggedIn', false);
                    break;
                }
                $sql = "SELECT fbID, message, Date, locationID FROM post WHERE postID='$postID'";
                $result = $db->query($sql, array('fbID','message', 'Date', 'locationID'));
                while ($row = $result->fetch()) {
                    $userID = $row['fbID'];
                    $message = $row['message'];
                    $date = $row['Date'];
                    $user = $this->controller->getUser($userID);
                    $photo = 'https://graph.facebook.com/' . $userID . '/picture';
                    //GET LOCATION
                    $locID = $row['locationID'];
                    $s = "SELECT acronym FROM location WHERE locationID = '$locID'";
                    $q = $db->query($s, array('acronym'));
                    if($r = $q->fetch()){
                        $loc = $r['acronym'];
                    }
                    else
                        $loc = "";
                
                    //LIKE SECTION
                    $session = $this->getSession();
                        if($this->isLoggedIn()){
                            $u = $session->getUser();
                            $uID = $u->getUserID();
                            $login = true;
                            $s = "SELECT * FROM likes WHERE postID = '$postID' AND fbID = '$uID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            if($r){
                                $likeimg = '/modules/posts/images/compliant/like.png';
                            }
                            else {
                                $likeimg = '/modules/posts/images/compliant/nolike.png';
                            }

                            //COUNT LIKES
                            $s = "SELECT COUNT(*) as likes FROM likes WHERE postID = '$postID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            $likes = $r['likes'];

                            //FLAG SECTION
                            $s = "SELECT * FROM flags WHERE postID = '$postID' AND fbID = '$uID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            if($r){
                                $flagimg = '/modules/posts/images/compliant/flag.png';
                            }
                            else {
                                $flagimg = '/modules/posts/images/compliant/noflag.png';
                            }
                        }
                        else{
                                $login = false;
                                $likeimg = false;
                                $likes = false;
                                $flagimg = false;
                        }           
                        //COUNT FLAGS
                        $s = "SELECT COUNT(*) as flags FROM flags WHERE postID = '$postID'";
                        $q = $db->query($s,array());
                        $r = $q->fetch();
                        $flags = $r['flags'];

                        if($flags < 5) {
                            $showpost = true;
                        }
                        else {
                            $showpost = false;
                        }
                            
                    $post = array(
                        'message'=>$message,
                        'username'=>$user['name'],
                        'img'=>$photo,
                        'postID'=>$postID,
                        'likeimg'=>$likeimg,
                        'likes'=>$likes,
                        'flagimg'=>$flagimg,
                        'reveal'=>$showpost,
                        'date'=>$date,
                        'loc'=>$loc,
                        'login'=>$login,
                        'url'=> $this->buildBreadcrumbURL('detail', array('postID'=>$postID))
                    );
                    $note[] = $post;
                }
                $this->assign('note', $note); 
                // GET REPLIES
                $replies = array();
                $sql = "SELECT reply_postID FROM replies WHERE to_postID = '$postID'";
                $result = $db->query($sql, array('reply_postID'));
                while($row = $result->fetch()){
                    $replyID = $row['reply_postID'];
                    $s = "SELECT fbID, message, Date FROM post WHERE postID='$replyID'";
                    $q = $db->query($s, array('fbID','message', 'Date'));
                    $r = $q->fetch();
                    $userID = $r['fbID'];
                    $message = $r['message'];
                    $date = $r['Date'];
                    $user = $this->controller->getUser($userID);
                    $photo = 'https://graph.facebook.com/' . $userID . '/picture';
                
                    //LIKE SECTION
                    $session = $this->getSession();
                        if($this->isLoggedIn()){
                            $u = $session->getUser();
                            $uID = $u->getUserID();
                            $login = true;
                            $s = "SELECT * FROM likes WHERE postID = '$replyID' AND fbID = '$uID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            if($r){
                                $likeimg = '/modules/posts/images/compliant/like.png';
                            }
                            else {
                                $likeimg = '/modules/posts/images/compliant/nolike.png';
                            }

                            //COUNT LIKES
                            $s = "SELECT COUNT(*) as likes FROM likes WHERE postID = '$replyID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            $likes = $r['likes'];

                            //FLAG SECTION
                            $s = "SELECT * FROM flags WHERE postID = '$replyID' AND fbID = '$uID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            if($r){
                                $flagimg = '/modules/posts/images/compliant/flag.png';
                            }
                            else {
                                $flagimg = '/modules/posts/images/compliant/noflag.png';
                            }
                        }
                        else{
                                $login = false;
                                $likeimg = false;
                                $likes = false;
                                $flagimg = false;
                        }           
                        //COUNT FLAGS
                        $s = "SELECT COUNT(*) as flags FROM flags WHERE postID = '$replyID'";
                        $q = $db->query($s,array());
                        $r = $q->fetch();
                        $flags = $r['flags'];

                        if($flags < 5) {
                            $showpost = true;
                        }
                        else {
                            $showpost = false;
                        }
                            
                    $post = array(
                        'message'=>$message,
                        'username'=>$user['name'],
                        'img'=>$photo,
                        'postID'=>$replyID,
                        'likeimg'=>$likeimg,
                        'likes'=>$likes,
                        'flagimg'=>$flagimg,
                        'reveal'=>$showpost,
                        'date'=>$date,
                        'login'=>$login
                    );
                    $replies[] = $post;
                }
                $this->assign('replies', $replies);
                $this->assign('reply', 1);
                if($this->isLoggedIn()){
                    $this->assign('loggedIn', true);
                }
                else
                    $this->assign('loggedIn', false);
                break;
            case 'popular':
                $locID = $this->getArg('locID');
                if(!$locID)
                    break;
                $db = new db();
                $sql = "SELECT postID FROM post WHERE locationID = '$locID'";
                $result = $db->query($sql, array('postID'));
                $totals = array();
                $postlist = array();
                while($row = $result->fetch()){
                    $postID = $row['postID'];
                    $s = "SELECT COUNT(*) as likes FROM likes WHERE postID = '$postID'";
                    $q = $db->query($s,array());
                    $r = $q->fetch();
                    $likes = $r['likes'];                  
                    
                    $s = "SELECT reply_postID FROM replies WHERE to_postID = '$postID'";
                    $q = $db->query($s, array('reply_postID'));
                    while($reply = $q->fetch()){
                        $replyID = $reply['reply_postID'];
                        $s = "SELECT COUNT(*) as likes FROM likes WHERE postID = '$replyID'";
                        $query = $db->query($s,array());
                        $r = $query->fetch(); 
                        $likes += $r['likes'];
                    }
                    $totals[] = array(
                        'postID' => $postID,
                        'likes' => $likes
                    );
                }
                usort($totals, array("NotesWebModule", "usort_callback"));
                
                $top3 = array_slice($totals, 0, 3);
                
                foreach ($top3 as $item)
                {
                    $postID = $item['postID'];
                    $sql = "SELECT fbID, message, postID, Date, locationID FROM post WHERE postID='$postID'";
                    $result = $db->query($sql,array('fbID', 'message', 'postID', 'Date', 'locationID'));
                    if($row = $result->fetch()){
                        $userID = $row['fbID'];
                        $postID = $row['postID'];
                        $message = $row['message'];
                        $date = $row['Date'];
                        $user = $this->controller->getUser($userID);
                        $photo = 'https://graph.facebook.com/' . $userID . '/picture';
                        //GET LOCATION
                        $locID = $row['locationID'];
                        $s = "SELECT acronym FROM location WHERE locationID = '$locID'";
                        $q = $db->query($s, array('acronym'));
                        if($r = $q->fetch()){
                            $loc = $r['acronym'];
                        }
                        else
                            $loc = "";

                        //LIKE SECTION
                        $session = $this->getSession();
                            if($this->isLoggedIn()){
                                $u = $session->getUser();
                                $uID = $u->getUserID();
                                $login = true;
                                $s = "SELECT * FROM likes WHERE postID = '$postID' AND fbID = '$uID'";
                                $q = $db->query($s,array());
                                $r = $q->fetch();
                                if($r){
                                    $likeimg = '/modules/posts/images/compliant/like.png';
                                }
                                else {
                                    $likeimg = '/modules/posts/images/compliant/nolike.png';
                                }

                                //COUNT LIKES
                                $s = "SELECT COUNT(*) as likes FROM likes WHERE postID = '$postID'";
                                $q = $db->query($s,array());
                                $r = $q->fetch();
                                $likes = $r['likes'];

                                //FLAG SECTION
                                $s = "SELECT * FROM flags WHERE postID = '$postID' AND fbID = '$uID'";
                                $q = $db->query($s,array());
                                $r = $q->fetch();
                                if($r){
                                    $flagimg = '/modules/posts/images/compliant/flag.png';
                                }
                                else {
                                    $flagimg = '/modules/posts/images/compliant/noflag.png';
                                }
                            }
                            else{
                                    $login = false;
                                    $likeimg = false;
                                    $likes = false;
                                    $flagimg = false;
                            }           
                        //COUNT FLAGS
                        $s = "SELECT COUNT(*) as flags FROM flags WHERE postID = '$postID'";
                        $q = $db->query($s,array());
                        $r = $q->fetch();
                        $flags = $r['flags'];

                        if($flags < 5) {
                            $showpost = true;
                        }
                        else {
                            $showpost = false;
                        }

                        //CHECK IF REPLY
                        $s = "SELECT reply_postID, to_postID FROM replies WHERE reply_postID = '$postID'";
                        $q = $db->query($s, array('reply_postID', 'to_postID'));
                        $r = $q->fetch();
                        if($r)
                            $url = $this->buildBreadcrumbURL('detail', array('postID'=>$r['to_postID']));
                        else
                            $url = $this->buildBreadcrumbURL('detail', array('postID'=>$postID));
                        $post = array(
                            'message'=>$message,
                            'username'=>$user['name'],
                            'img'=>$photo,
                            'postID'=>$postID,
                            'likeimg'=>$likeimg,
                            'likes'=>$likes,
                            'flagimg'=>$flagimg,
                            'reveal'=>$showpost,
                            'date'=>$date,
                            'loc'=>$loc,
                            'login'=>$login,
                            'url'=> $url
                        );
                       $postlist[] = $post;
                    }
                }
                $this->assign('postlist', $postlist);
                break;
        }
    }
}